import xlrd
import xlwt
from xlutils.copy import copy
import time


class ExcelWriter:
    def __init__(self):
        self.file_name = "E:/data/swimsuit&underwear/Archive/swimsuit&underwear.xls"

    def write_excel_xls_append(self, file_names):
        index = len(file_names)  # 获取需要写入数据的行数
        workbook = xlrd.open_workbook(self.file_name, formatting_info=True)  # 打开工作簿
        sheets = workbook.sheet_names()  # 获取工作簿中的所有表格
        worksheet = workbook.sheet_by_name(sheets[0])  # 获取工作簿中所有表格中的的第一个表格
        rows_old = worksheet.nrows  # 获取表格中已存在的数据的行数
        new_workbook = copy(workbook)  # 将xlrd对象拷贝转化为xlwt对象
        new_worksheet = new_workbook.get_sheet(0)  # 获取转化后工作簿中的第一个表格
        for i in range(0, index):
            new_worksheet.write(i + rows_old, 0, file_names[i])
            new_worksheet.write(i + rows_old, 1, time.strftime("%Y/%m/%d", time.localtime()))
        new_workbook.save(self.file_name)  # 保存工作簿
        print("xls格式表格【追加】写入数据成功！")

